package com.wzy.netty.core.protocoltcp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * 描述  :
 * 创建人:    Wzy
 * 创建时间: 2020-05-23  1:41
 */
public class MyMessageDecoder extends ReplayingDecoder<Void> {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("MyMessageDecoder被调用...");
        //byte --> messageProtocol转换
        int length = in.readInt();
        byte[] content = new byte[length];
        in.readBytes(content);
        //封装messageProtocol对象 放入out 传入下一个handler
        MessageProtocol messageProtocol = new MessageProtocol();
        messageProtocol.setLen(length);
        messageProtocol.setContent(content);

        out.add(messageProtocol);

    }
}
